package com.hetungo.erpapp;

import android.content.Intent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    int REQUEST_CODE_SCAN = 111;
    WebView webView;
//    String targetUrl = "file:///android_asset/index.html";
    String targetUrl = "http://132.232.19.19/ok/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.webView = this.findViewById(R.id.wv);
        WebSettings webSettings= this.webView.getSettings();
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptEnabled(true);//WebView支持调用Js
        webSettings.setDomStorageEnabled(true);
        this.webView.setWebViewClient(new WebViewClient());
        this.webView.clearCache(true);


        this.webView.loadUrl(targetUrl);
        this.webView.addJavascriptInterface(MainActivity.this,"android");
    }

    @JavascriptInterface
    public void jsCallAndroid(){
        javaCallJs("test");
    }

    @JavascriptInterface
    public void jsCallCamera(){
        AndPermission.with(this)
                .permission(Permission.CAMERA,Permission.READ_EXTERNAL_STORAGE)
                .onGranted(new Action() {
                    @Override
                    public void onAction(List<String> permissions) {
                        try{

                            Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                            /*ZxingConfig是配置类
                             *可以设置是否显示底部布局，闪光灯，相册，
                             * 是否播放提示音  震动
                             * 设置扫描框颜色等
                             * 也可以不传这个参数
                             * */
                            ZxingConfig config = new ZxingConfig();
                            // config.setPlayBeep(false);//是否播放扫描声音 默认为true
                            //  config.setShake(false);//是否震动  默认为true
                            // config.setDecodeBarCode(false);//是否扫描条形码 默认为true
//                                config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
//                                config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
//                                config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
                            config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
                            intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
                            startActivityForResult(intent, REQUEST_CODE_SCAN);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).onDenied(new Action() {
            @Override
            public void onAction(List<String> permissions) {
                StringBuilder temp = new StringBuilder();
                for(String p : permissions){
                    temp.append(p);
                }
                final String text = temp.toString();
                webView.post(new Runnable() {
                    @Override
                    public void run() {
                        webView.loadUrl("javascript:javaCallJs('"+text+"')");
                    }
                });
//                        Uri packageURI = Uri.parse("package:" + getPackageName());
//                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//
//                        startActivity(intent);

                Toast.makeText(MainActivity.this, "没有权限无法扫描呦", Toast.LENGTH_LONG).show();
            }
        }).start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // 扫描二维码/条码回传
        if (requestCode == REQUEST_CODE_SCAN && resultCode == RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                javaCallJs(content);
            }
        }
    }


    public void javaCallJs(final String value){
        webView.post(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl("javascript:javaCallJs('"+value+"')");
            }
        });
    }
}
